#include "form_xxcx.h"
#include "ui_form_xxcx.h"
#include "pub_var.h"

#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonArray>
#include <QSettings>
#include <QMessageBox>
#include <QDebug>
#include <QSqlError>
#include <QSqlRecord>
#include <QDateTime>
#include <QFile>
#include <QFileDialog>

Form_xxcx::Form_xxcx(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form_xxcx)
{
    ui->setupUi(this);

    QSettings setIni("clin.ini", QSettings::IniFormat);
    //恢复当前窗口位置
    QRect rct = setIni.value("windowState/xxcx").toRect();
    this->setGeometry(rct);

    //数据库连接
    qrm = new QSqlQuery(pub::db_m);
    qro = new QSqlQuery(pub::db_o);
    qrot = new QSqlQuery(pub::db_o);

    //默认日期
    QDate dt;
    ui->deStart->setDate(dt.currentDate());
    ui->deEnd->setDate(dt.currentDate());

    request.setUrl(QUrl(pub::hos_ybjy));
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");

    tm = new QTimer(this);
    connect(tm, SIGNAL(timeout()), this, SLOT(onTimeOut()));
    ui->leJzid->setEnabled(false);
}

Form_xxcx::~Form_xxcx()
{
    delete ui;
}

void Form_xxcx::closeEvent(QCloseEvent *event)
{
    Q_UNUSED(event);
    QSettings setIni("clin.ini", QSettings::IniFormat);
    setIni.setValue("windowState/xxcx", QVariant(this->geometry()));

}


void Form_xxcx::on_pb5201_clicked()
{
    //就诊信息查询
    curMsg = "执行 5201 交易 。。。";
    tm->start(300);
    QJsonObject in;
    putBaseIn(  in, "5201") ;

    QJsonObject inpu,inpud;

    inpu.insert("psn_no", psn_no);//
    inpu.insert("begntime", ui->deStart->date().toString("yyyy-MM-dd")+" 00:00:00");//开始日期
    inpu.insert("endtime", ui->deEnd->date().toString("yyyy-MM-dd")+" 23:59:59");//结束日期
    inpu.insert("med_type", ui->cbbYllb->currentText());//医疗类别
    //inpu.insert("mdtrt_id", "2100659508");//


    inpud.insert("data",inpu);
    in.insert("input", inpud);

    QJsonDocument indoc ;
    indoc.setObject(in);
    qDebug()<<"5201 indoc "<<indoc;

    //保存入参日志
    sq = "update com_business set v_in = '"
            + indoc.toJson(QJsonDocument::Compact)
            + "' where jyls = '" + currentMsgid + "'";

    if(!qrm->exec(sq)){
        QMessageBox::information(this,"错误","保存入参日志失败！");
        qDebug()<<sq;
    }

    QNetworkReply* reply = manager.post(request,indoc.toJson());

    QEventLoop eventLoop;
    QObject::connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
    eventLoop.exec(); // 进入等待返回，但ui事件循环依然进行。

    qDebug()<<" request 5201 is returned";

    QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);

    if(!statusCode.isValid()){
        QMessageBox::warning(NULL,"注意","http 请求状态错误: " + statusCode.toString());
        tm->stop();
        return ;
    }

    QNetworkReply::NetworkError err = reply->error();

    if(err != QNetworkReply::NoError){
        QMessageBox::warning(NULL,"注意",reply->errorString());
        tm->stop();
        return ;
    }

    QByteArray bt = reply->readAll();
    QString t = QString::fromLocal8Bit(bt);

    QJsonParseError json_error;
    QJsonDocument jsdc = QJsonDocument::fromJson(t.toUtf8(), &json_error);
    if(json_error.error != 0 ){
        QMessageBox::warning(NULL,"注意","返回信息解析错误 " + json_error.errorString());
        tm->stop();
        return ;
    }
    qDebug()<< "5201 outDoc "<<jsdc;
    QJsonObject jsob = jsdc.object();

    sq = "update com_business set v_out = '" + jsdc.toJson(QJsonDocument::Compact) + "'"
         " where jyls = '" + currentMsgid + "'";

    if(!qrm->exec(sq)){
        QMessageBox::information(this,"错误","保存出参日志失败！");
        qDebug()<<sq;
    }

    if(jsob.value("infcode").toInt() != 0){
        QString t = jsob.value("err_msg").toString();
        QMessageBox::warning(NULL,"注意","5201 交易失败：" + t );
        tm->stop();
        return ;
    }
    ret5201 = jsob.value("output").toObject().value("mdtrtinfo").toArray();
    ui->lwResult->clear();
    ui->lwResult->addItem( "mdtrt_id \t insutype \t psn_type \t med_type \t begntime \t endtime");
    for(int i = 0; i< ret5201.size();  i++){
        ui->lwResult->addItem( ret5201[i].toObject().value("mdtrt_id").toString()
                               + "\t"
                               + ret5201[i].toObject().value("insutype").toString()
                               + "\t"
                               + ret5201[i].toObject().value("psn_type").toString()
                               + "\t"
                               + ret5201[i].toObject().value("med_type").toString()
                               + "\t"
                               + ret5201[i].toObject().value("begntime").toString()
                               + "\t"
                               + ret5201[i].toObject().value("endtime").toString()

                               );
    }
    tm->stop();
    QMessageBox::information(this,"注意","查询结束！");

}

void Form_xxcx::on_pb5202_clicked()
{
    //诊断信息查询
    QJsonObject in;
    putBaseIn(  in, "5202") ;

    QJsonObject inpu,inpud;

    inpu.insert("psn_no", psn_no);//
    inpu.insert("mdtrt_id", ui->cbbJzid->currentText());//


    inpud.insert("data",inpu);
    in.insert("input", inpud);

    QJsonDocument indoc ;
    indoc.setObject(in);
    qDebug()<<"5202 indoc "<<indoc;

    //保存入参日志
    sq = "update com_business set v_in = '"
            + indoc.toJson(QJsonDocument::Compact)
            + "' where jyls = '" + currentMsgid + "'";

    if(!qrm->exec(sq)){
        QMessageBox::information(this,"错误","保存入参日志失败！");
        qDebug()<<sq;
    }

    QNetworkReply* reply = manager.post(request,indoc.toJson());

    QEventLoop eventLoop;
    QObject::connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
    eventLoop.exec(); // 进入等待返回，但ui事件循环依然进行。

    qDebug()<<" request 5202 is returned";

    QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);

    if(!statusCode.isValid()){
        QMessageBox::warning(NULL,"注意","http 请求状态错误: " + statusCode.toString());
        return ;
    }

    QNetworkReply::NetworkError err = reply->error();

    if(err != QNetworkReply::NoError){
        QMessageBox::warning(NULL,"注意",reply->errorString());
        return ;
    }

    QByteArray bt = reply->readAll();
    QString t = QString::fromLocal8Bit(bt);

    QJsonParseError json_error;
    QJsonDocument jsdc = QJsonDocument::fromJson(t.toUtf8(), &json_error);
    if(json_error.error != 0 ){
        QMessageBox::warning(NULL,"注意","返回信息解析错误 " + json_error.errorString());

        return ;
    }
    qDebug()<< "5202 outDoc "<<jsdc;
    QJsonObject jsob = jsdc.object();

    sq = "update com_business set v_out = '" + jsdc.toJson(QJsonDocument::Compact) + "'"
         " where jyls = '" + currentMsgid + "'";

    if(!qrm->exec(sq)){
        QMessageBox::information(this,"错误","保存出参日志失败！");
        qDebug()<<sq;
    }

    if(jsob.value("infcode").toInt() != 0){
        QString t = jsob.value("err_msg").toString();
        QMessageBox::warning(NULL,"注意","5202 交易失败：" + t );

        return ;
    }
    ret5202 = jsob.value("output").toObject().value("diseinfo").toArray();
    ui->lwResult->clear();
    ui->lwResult->addItem("diag_info_id \t diag_code \t diag_name \t dise_dor_nam");
    for(int i = 0; i< ret5202.size();  i++){
        ui->lwResult->addItem( ret5202[i].toObject().value("diag_info_id").toString()
                               +"\t"
                               +ret5202[i].toObject().value("diag_code").toString()
                               +"\t"
                               +ret5202[i].toObject().value("diag_name").toString()
                               +"\t"
                               +ret5202[i].toObject().value("dise_dor_nam").toString()
                               );

    }

    QMessageBox::information(this,"注意","查询结束！");
}

void Form_xxcx::on_pb5203_clicked()
{
    //结算信息查询
    QJsonObject in;
    putBaseIn(  in, "5203") ;

    QJsonObject inpu,inpud;

    inpu.insert("psn_no", psn_no);//
    inpu.insert("setl_id", ui->cbbJsid->currentText());//
    inpu.insert("mdtrt_id", ui->cbbJzid->currentText());//


    inpud.insert("data",inpu);
    in.insert("input", inpud);

    QJsonDocument indoc ;
    indoc.setObject(in);
    qDebug()<<"5203 indoc "<<indoc;

    //保存入参日志
    sq = "update com_business set v_in = '"
            + indoc.toJson(QJsonDocument::Compact)
            + "' where jyls = '" + currentMsgid + "'";

    if(!qrm->exec(sq)){
        QMessageBox::information(this,"错误","保存入参日志失败！");
        qDebug()<<sq;
    }

    QNetworkReply* reply = manager.post(request,indoc.toJson());

    QEventLoop eventLoop;
    QObject::connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
    eventLoop.exec(); // 进入等待返回，但ui事件循环依然进行。

    qDebug()<<" request 5203 is returned";

    QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);

    if(!statusCode.isValid()){
        QMessageBox::warning(NULL,"注意","http 请求状态错误: " + statusCode.toString());
        return ;
    }

    QNetworkReply::NetworkError err = reply->error();

    if(err != QNetworkReply::NoError){
        QMessageBox::warning(NULL,"注意",reply->errorString());
        return ;
    }

    QByteArray bt = reply->readAll();
    QString t = QString::fromLocal8Bit(bt);

    QJsonParseError json_error;
    QJsonDocument jsdc = QJsonDocument::fromJson(t.toUtf8(), &json_error);
    if(json_error.error != 0 ){
        QMessageBox::warning(NULL,"注意","返回信息解析错误 " + json_error.errorString());

        return ;
    }
    qDebug()<< "5203 outDoc "<<jsdc;
    QJsonObject jsob = jsdc.object();

    sq = "update com_business set v_out = '" + jsdc.toJson(QJsonDocument::Compact) + "'"
         " where jyls = '" + currentMsgid + "'";

    if(!qrm->exec(sq)){
        QMessageBox::information(this,"错误","保存出参日志失败！");
        qDebug()<<sq;
    }

    if(jsob.value("infcode").toInt() != 0){
        QString t = jsob.value("err_msg").toString();
        QMessageBox::warning(NULL,"注意","5203 交易失败：" + t );

        return ;
    }

    ret5203info = jsob.value("output").toObject().value("setlinfo").toObject();
    ret5203detail = jsob.value("output").toObject().value("setldetail").toArray();

    qDebug()<<ret5203info;
               qDebug()<<jsob.value("output").toObject();

    ui->lwResult->clear();

    ui->lwResult->addItem( "总额:"
                           + QString::asprintf("%.2f",ret5203info.value("medfee_sumamt").toDouble())
                           + "  基金支出:"
                           + QString::asprintf("%.2f",ret5203info.value("hifp_pay").toDouble())
                           + "  个人账户支出:"
                           + QString::asprintf("%.2f",ret5203info.value("acct_pay").toDouble())
                           + "  现金支出:"
                           + QString::asprintf("%.2f",ret5203info.value("cash_payamt").toDouble())
                           + "  余额:"
                           + QString::asprintf("%.2f",ret5203info.value("balc").toDouble())
                           + "  结算时间:"
                           + ret5203info.value("setl_time").toString()

                           );
    ui->lwResult->addItem( "diag_info_id \t diag_code \t diag_name");
    for(int i = 0; i< ret5203detail.size();  i++){

        ui->lwResult->addItem( ret5203detail[i].toObject().value("diag_info_id").toString()
                               +"\t"
                               +ret5203detail[i].toObject().value("diag_code").toString()
                               +"\t"
                               +ret5203detail[i].toObject().value("diag_name").toString());
    }

    QMessageBox::information(this,"注意","查询结束！");
}
void Form_xxcx::on_pb2601_clicked()
{

    //核实操作确认
    QMessageBox:: StandardButton result= QMessageBox::warning(NULL, "注意", "此操作不可撤销，确定要继续？",QMessageBox::Yes|QMessageBox::No);
    if(result == QMessageBox::No){
        return;
    }

    QJsonObject in;
    putBaseIn(  in, "2601") ;

    QJsonObject inpu,inpud;

    inpu.insert("psn_no", ui->lePsnno->text());//psn_no
    inpu.insert("omsgid", ui->leMsgid->text());//
    inpu.insert("oinfno", oinfno);//


    inpud.insert("data",inpu);
    in.insert("input", inpud);

    QJsonDocument indoc ;
    indoc.setObject(in);
    qDebug()<<"2601 indoc "<<indoc;

    //保存入参日志
    sq = "update com_business set v_in = '"
            + indoc.toJson(QJsonDocument::Compact)
            + "' where jyls = '" + currentMsgid + "'";

    if(!qrm->exec(sq)){
        QMessageBox::information(this,"错误","保存入参日志失败！");
        qDebug()<<sq;
    }

    QNetworkReply* reply = manager.post(request,indoc.toJson());

    QEventLoop eventLoop;
    QObject::connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
    eventLoop.exec(); // 进入等待返回，但ui事件循环依然进行。

    qDebug()<<" request 2601 is returned";

    QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);

    if(!statusCode.isValid()){
        QMessageBox::warning(NULL,"注意","http 请求状态错误: " + statusCode.toString());
        return ;
    }

    QNetworkReply::NetworkError err = reply->error();

    if(err != QNetworkReply::NoError){
        QMessageBox::warning(NULL,"注意",reply->errorString());
        return ;
    }

    QByteArray bt = reply->readAll();
    QString t = QString::fromLocal8Bit(bt);

    QJsonParseError json_error;
    QJsonDocument jsdc = QJsonDocument::fromJson(t.toUtf8(), &json_error);
    if(json_error.error != 0 ){
        QMessageBox::warning(NULL,"注意","返回信息解析错误 " + json_error.errorString());

        return ;
    }
    qDebug()<< "2601 outDoc "<<jsdc;
    QJsonObject jsob = jsdc.object();

    sq = "update com_business set v_out = '" + jsdc.toJson(QJsonDocument::Compact) + "'"
         " where jyls = '" + currentMsgid + "'";

    if(!qrm->exec(sq)){
        QMessageBox::information(this,"错误","保存出参日志失败！");
        qDebug()<<sq;
    }


    if(jsob.value("infcode").toInt() != 0){
        QString t = jsob.value("err_msg").toString();
        QMessageBox::warning(NULL,"注意","2601 交易失败：" + t );
        return ;
    }


    QMessageBox::information(this,"注意","冲正操作结束！");
}

void Form_xxcx::on_pbSearch_clicked()
{
    QString codeList = "'code'";

    ui->lwMsgid->clear();
    if(clinic_code == "'code'"){
        QMessageBox::information(this,"注意","无效的就诊信息！");
        return;
    }
    QString cond = "";
    //if(ui->cbCz->isChecked()){  //可以被冲正的交易
    cond = " ywbh in ('2207A','2208') and ";
    //}

    if(ui->rbCur->isChecked()){

        sq = "select distinct clinic_code from fin_opr_register where "
             " card_no = '"
                + pub::add0(ui->leMzh->text(), 10)
                + "' and reg_date > to_date('"
                + ui->deStart->date().toString("yyyy-MM-dd")
                +"','yyyy/mm/dd') and reg_date< to_date('"
                + ui->deEnd->date().toString("yyyy-MM-dd")
                +" 23:59:59','yyyy/mm/dd hh24:mi:ss')" ;

        qro->exec(sq);
        while(qro->next()){
            codeList.append(",'");
            codeList.append(qro->value(0).toString());
            codeList.append("'");
        }
        qDebug()<<sq;

        sq = "select jyls, jysj, ywbh from com_business where "
                + cond + " clinic_code in ("
                + codeList + ") order by atinc";

    }else if(ui->rbAll->isChecked()){
        sq = "select jyls, jysj, ywbh from com_business where "
                + cond
                + " jysj > '"
                + ui->deStart->date().toString("yyyy-MM-dd")
                +" 00:00:00' and jysj< '"
                + ui->deEnd->date().toString("yyyy-MM-dd")
                +" 23:59:59' order by atinc";

    }else if(ui->rbInvalid->isChecked()){

        sq = "select msgid from fin_ipr_siinmaininfo where oper_date > to_date('"
                + ui->deStart->date().toString("yyyy-MM-dd")
                +"','yyyy/mm/dd') and oper_date< to_date('"
                + ui->deEnd->date().toString("yyyy-MM-dd")
                +" 23:59:59','yyyy/mm/dd hh24:mi:ss')";
        qro->exec(sq);
        QString lst = "'#a#'";
        while(qro->next()){
            lst.append(",'");
            lst.append(qro->value(0).toString());
            lst.append("'");
        }
        qDebug() << sq;
        qDebug() << lst;

        sq = "select jyls, jysj, ywbh from com_business where "
                + cond
                + " jysj > '"
                + ui->deStart->date().toString("yyyy-MM-dd")
                +" 00:00:00' and jysj< '"
                + ui->deEnd->date().toString("yyyy-MM-dd")
                +" 23:59:59' and jyls not in("
                + lst + " ) order by atinc";
    }else{
        QMessageBox::information(this,"注意","无效选择状态！");
        return;
    }

    qrm->exec(sq);
    qDebug()<<sq;
    while(qrm->next()){
        ui->lwMsgid->addItem( qrm->value(0).toString()
                               +"\t"
                               +qrm->value(1).toString()
                               +"\t"
                               +qrm->value(2).toString()
                               );
    }
}

void Form_xxcx::on_pbExit_clicked()
{
    this->close();
}

void Form_xxcx::putBaseIn( QJsonObject &in, QString code)
{
    QString inp;
    inp = pub::getYbStrJ(code,"");   //
    if(inp.isEmpty()){
        //pub::useInsurance = false;  //设置医保不可用
        QMessageBox::information(this,"note","申请交易流水号失败！");
        return; //未能成功申请交易流水号
    }

    currentMsgid = inp;

    in.insert("infno", code);
    in.insert( "msgid", inp);
    in.insert("mdtrtarea_admvs", pub::hos_insuplc);
    //in.insert("insuplc_admdvs", mpPerson["insuplc_admdvs"] );

    in.insert("insuplc_admdvs","210100");
    in.insert("recer_sys_code", "OwnSoft");
    in.insert("dev_no","");
    in.insert("dev_safe_info","");
    in.insert("cainfo", pub::hos_secretcode);
    in.insert("signtype","");
    in.insert("infver", "V1.0");
    in.insert("opter_type", "1");
    in.insert("opter", pub::usr_code);
    in.insert("opter_name", pub::usr_name);
    in.insert("inf_time", QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"));
    in.insert("fixmedins_code", pub::hos_ybbh);
    in.insert("fixmedins_name", pub::hos_name);
    in.insert("sign_no", pub::sign_no);

}


void Form_xxcx::on_pbMzh_clicked()
{
    if(ui->leMzh->text().isEmpty()){
        return;
    }
    sq = "select distinct psn_no, mdtrt_id, name, inpatient_no from fin_ipr_siinmaininfo"
         " where card_no = '"
            + pub::add0(ui->leMzh->text(), 10)
            + "' and in_date > to_date('"
            + ui->deStart->date().toString("yyyy-MM-dd")
            +"','yyyy/mm/dd') and in_date< to_date('"
            + ui->deEnd->date().toString("yyyy-MM-dd")
            +" 23:59:59','yyyy/mm/dd hh24:mi:ss')" ;
    qDebug()<<sq;
    qro->exec(sq);
    if(!qro->next()){
        QMessageBox::information(this,"注意","找不到指定的门诊号信息！");
        return;
    }
    ui->cbbJsid->clear();
    ui->cbbJzid->clear();
    while(true){

        if(!qro->value(2).toString().isEmpty()){
            ui->leXm->setText( qro->value(2).toString() );
            clinic_code = qro->value(3).toString();

        }
        if(!qro->value(0).toString().isEmpty()){
            psn_no = qro->value(0).toString();
        }
        if(!qro->value(1).toString().isEmpty()){
            ui->cbbJzid->addItem( qro->value(1).toString());
        }
        if(!qro->next()){
            break;
        }
    }

}
void Form_xxcx::on_cbbJzid_currentTextChanged(QString str)
{
    if(str.isEmpty()){
        return;
    }
    sq = "select setl_id,inpatient_no,invoice_no from fin_ipr_siinmaininfo"
         " where psn_no = '"
            + psn_no
            + "' and mdtrt_id = '"
            +str
            +"' and in_date > to_date('"
            + ui->deStart->date().toString("yyyy-MM-dd")
            +"','yyyy/mm/dd') and in_date< to_date('"
            + ui->deEnd->date().toString("yyyy-MM-dd")
            +" 23:59:59','yyyy/mm/dd hh24:mi:ss')" ;
    qDebug()<<sq;
    qrot->exec(sq);

    ui->cbbJsid->clear();
    while(qrot->next()){
        ui->cbbJsid->addItem( qrot->value(0).toString());
    }
}

void Form_xxcx::onTimeOut()
{
    if(ui->leJzid->text().isEmpty()){
        ui->leJzid->setText(curMsg);
    }else{
        ui->leJzid->clear();
    }
    qApp->processEvents();
}


void Form_xxcx::on_lwMsgid_doubleClicked(const QModelIndex &index)
{

    ui->leMsgid->setText( pub::getStr(ui->lwMsgid->item(index.row())->text(),"\t",1));
    oinfno = pub::getStr(ui->lwMsgid->item(index.row())->text(),"\t",3).left(4);    //有些编号带 A, 过滤掉。

    sq = "select v_in, v_out from com_business where jyls = '" + ui->leMsgid->text() + "'";
    qrm->exec(sq);
    qrm->next();
    QJsonDocument jsdc = QJsonDocument::fromJson(qrm->value(0).toString().toUtf8());
    QJsonObject jsob = jsdc.object();

    if(oinfno == "2207"){
        psn_no = jsob.value("input").toObject().value("data").toObject().value("psn_no").toString();
        ui->lePsnno->setText(psn_no);

    }else if(oinfno == "2208"){

    }else{

    }

    if(!ui->rbInvalid->isChecked()){        //非无效msgid，查询对应数据
        QJsonDocument jsdc = QJsonDocument::fromJson(qrm->value(1).toString().toUtf8());
        QJsonObject jsob = jsdc.object();
        QString stid = jsob.value("output").toObject().value("setlinfo").toObject().value("setl_id").toString();

        if(stid.isEmpty()){
            QMessageBox::information(this,"note","查询 setl_id 失败！");
            return; //未能成功申请交易流水号
        }
        sq = "select invoice_no, medfee_sumamt, fund_pay_sumamt, acct_pay, psn_cash_pay, non_insu_amount "
             "from fin_ipr_siinmaininfo where setl_id = '" + stid + "'";
        qro->exec(sq);
        if(qro->next()){
            QString pay = "支付总额：" + qro->value(1).toString()
                    + "    统筹：" + qro->value(2).toString()
                    + "    账户：" + qro->value(3).toString()
                    + "    现金：" + qro->value(4).toString()
                    + "    非医保支付金额：" + qro->value(5).toString();
            sq = "select recipe_no, doct_code,item_name, specs, qty, price_unit, own_cost "
                 " from fin_opb_feedetail where invoice_no = '"
                 + qro->value(0).toString() + "' order by recipe_no, sequence_no";
            qro->exec(sq);

            ui->lwResult->clear();
            ui->lwResult->addItem( "处方号\t 医生代码\t 项目名称\t 规格\t 数量\t 价格\t 金额");

            while(qro->next()){
                ui->lwResult->addItem( qro->value(0).toString()
                                       + "\t"
                                       + qro->value(1).toString()
                                       + "\t"
                                       + qro->value(2).toString()
                                       + "\t"
                                       + qro->value(3).toString()
                                       + "\t"
                                       + qro->value(4).toString()
                                       + "\t"
                                       + qro->value(5).toString()
                                       + "\t"
                                       + qro->value(6).toString());

            }
            ui->lwResult->addItem( "\n" );
            ui->lwResult->addItem( pay );
        }else{
            QMessageBox::information(this,"note","查询 invoice_no 失败！");
            return;
        }

    }



}

void Form_xxcx::on_pbDycx_clicked()
{
    //待遇查询
    QJsonObject in;
    putBaseIn(  in, "2001") ;

    QJsonObject inpu,inpud;

    inpu.insert("psn_no", psn_no);//
    inpu.insert("insutype", ui->cbbXz->currentText());   //险种类型
    inpu.insert("fixmedins_code", pub::hos_ybbh); //定点医药机构编号
    inpu.insert("med_type", "11"); //医疗类别
    inpu.insert("begntime", ui->deStart->date().toString("yyyy-MM-dd")+" 00:00:00");       //开始时间

    inpud.insert("data",inpu);
    in.insert("input", inpud);

    QJsonDocument indoc ;
    indoc.setObject(in);
    qDebug()<<"2001 indoc "<<indoc;

    //保存入参日志
    sq = "update com_business set v_in = '"
            + indoc.toJson(QJsonDocument::Compact)
            + "' where jyls = '" + currentMsgid + "'";

    if(!qrm->exec(sq)){
        QMessageBox::information(this,"错误","保存入参日志失败！");
        qDebug()<<sq;
    }

    QNetworkReply* reply = manager.post(request,indoc.toJson());

    QEventLoop eventLoop;
    QObject::connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
    eventLoop.exec(); // 进入等待返回，但ui事件循环依然进行。

    qDebug()<<" request 2001 is returned";

    QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);

    if(!statusCode.isValid()){
        QMessageBox::warning(NULL,"注意","http 请求状态错误: " + statusCode.toString());
        return ;
    }

    QNetworkReply::NetworkError err = reply->error();

    if(err != QNetworkReply::NoError){
        QMessageBox::warning(NULL,"注意",reply->errorString());
        return ;
    }

    QByteArray bt = reply->readAll();
    QString t = QString::fromLocal8Bit(bt);

    QJsonParseError json_error;
    QJsonDocument jsdc = QJsonDocument::fromJson(t.toUtf8(), &json_error);
    if(json_error.error != 0 ){
        QMessageBox::warning(NULL,"注意","返回信息解析错误 " + json_error.errorString());

        return ;
    }
    qDebug()<< "2001 outDoc "<<jsdc;
    QJsonObject jsob = jsdc.object();

    sq = "update com_business set v_out = '" + jsdc.toJson(QJsonDocument::Compact) + "'"
         " where jyls = '" + currentMsgid + "'";

    if(!qrm->exec(sq)){
        QMessageBox::information(this,"错误","保存出参日志失败！");
        qDebug()<<sq;
    }

    if(jsob.value("infcode").toInt() != 0){
        QString t = jsob.value("err_msg").toString();
        QMessageBox::warning(NULL,"注意","2001 交易失败：" + t );

        return ;
    }

    QJsonArray jsar = jsob.value("output").toObject().value("trtinfo").toArray();

    ui->lwResult->clear();
    ui->lwResult->addItem("trt_chk_type\tfund_pay_type\ttrt_enjymnt_flag\tbegndate\tenddate\ttrt_chk_rslt");

    QJsonArray::iterator it = jsar.begin();

    while(it != jsar.end()){

        QString tmp = it->toObject().value("trt_chk_type").toString();
        tmp.append("\t");
        tmp.append(it->toObject().value("fund_pay_type").toString());
        tmp.append("\t");
        tmp.append(it->toObject().value("trt_enjymnt_flag").toString());
        tmp.append("\t");
        tmp.append(it->toObject().value("begndate").toString());
        tmp.append("\t");
        tmp.append(it->toObject().value("enddate").toString());
        tmp.append("\t");
        tmp.append(it->toObject().value("trt_chk_rslt").toString());

        ui->lwResult->addItem(tmp);
        it ++ ;
    }
    QMessageBox::information(this,"注意","查询结束！");
}

void Form_xxcx::on_pbCalc_clicked()
{
    sq = "select distinct card_no from fin_ipr_siinmaininfo where "
         " insutype = '"
            + ui->cbbXz->currentText()
            + "' and in_date > to_date('"
            + ui->deStart->date().toString("yyyy-MM-dd")
            +"','yyyy/mm/dd') and in_date< to_date('"
            + ui->deEnd->date().toString("yyyy-MM-dd")
            +" 23:59:59','yyyy/mm/dd hh24:mi:ss') order by card_no" ;

    qro->exec(sq);
    ui->lwMzh->clear();
    while(qro->next()){
        ui->lwMzh->addItem(qro->value(0).toString());

    }
    qDebug()<<sq;

}

void Form_xxcx::on_lwMzh_currentTextChanged(QString s)
{
    ui->leMzh->setText(s);
}
